﻿Imports System.Data.SqlClient
Imports System.IO

Public Class frmClientRegistry

    Public SNo As Integer


    Sub FillProducersList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim ProducersList As ArrayList = GetProducersList()

            Me.CombProducers.Items.Clear()

            For Each InsurComp As String In ProducersList
                Me.CombProducers.Items.Add(InsurComp)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillClientsList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cond As String
            If Me.RClientName.Checked = True Then
                cond = "Where FullName Like N'%" & Me.txtClientNameSearch.Text.Trim & "%'"

            ElseIf Me.RProducer.Checked = True Then
                cond = "Where Producer=N'" & Me.CombProducers.SelectedItem & "'"

            ElseIf Me.RAll.Checked = True Then
                cond = ""
            End If

            Dim cmd As New SqlCommand("Select SNo,IsNull(FullName,N'') FullName,IsNull(Producer,N'') Producer," & _
                                      "IsNull(Tele,N'') Tele,IsNull(Address,N'') Address " & _
                                      "From Clients " & cond & " Order By SNo", cnn)
            Dim Reader As SqlDataReader

            Me.GridClients.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.GridClients.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("FullName"), Reader.Item("Producer"), _
                                                      Reader.Item("Tele"), _
                                                      Reader.Item("Address"), "Edit"})
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmClientNew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

        FillProducersList()

        'Check 

    End Sub

    Private Sub txtClientNameSearch_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtClientNameSearch.KeyUp
        If e.KeyCode = Keys.Enter Then
            FillClientsList()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FillClientsList()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cond As String
            If Me.RClientName.Checked = True Then
                cond = "Where FullName Like N'%" & Me.txtClientNameSearch.Text.Trim & "%'"

            ElseIf Me.RProducer.Checked = True Then
                cond = "Where Producer=N'" & Me.CombProducers.SelectedItem & "'"

            ElseIf Me.RAll.Checked = True Then
                cond = ""
            End If

            Dim dap As New SqlDataAdapter("Select * From Clients " & cond & " Order By SNo", cnn)
            Dim das As New DataSet

            dap.Fill(das, "Clients")

            Dim rpt As New ClientsList
            rpt.SetDataSource(das)
            rptViewer.CrystalReportViewer1.ReportSource = rpt
            rptViewer.ShowDialog()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub CheckRadio()
        If Me.RClientName.Checked = True Then
            Me.txtClientNameSearch.Enabled = True
            Me.CombProducers.Enabled = False
            Me.CombProducers.SelectedIndex = -1
            Me.txtClientNameSearch.Focus()

        ElseIf Me.RProducer.Checked = True Then
            Me.CombProducers.Enabled = True
            Me.CombProducers.SelectedIndex = -1
            Me.txtClientNameSearch.Enabled = False
            Me.txtClientNameSearch.Clear()

        ElseIf Me.RAll.Checked = True Then
            Me.CombProducers.Enabled = False
            Me.CombProducers.SelectedIndex = -1
            Me.txtClientNameSearch.Enabled = False
            Me.txtClientNameSearch.Clear()
        End If
    End Sub

    Private Sub RAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RAll.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub RProducer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RProducer.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub GridClients_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridClients.CellClick
        If e.ColumnIndex = Me.DGColEdit.Index Then
            Dim a As New frmClientEdit
            a.SNo = Me.GridClients.CurrentRow.Cells(0).Value
            a.txtClientNo.Text = Me.GridClients.CurrentRow.Cells(0).Value
            a.FillContactsList()
            a.ShowDialog()
            FillClientsList()

        End If
    End Sub

    Private Sub RClientName_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RClientName.CheckedChanged
        CheckRadio()
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Dim a As New frmClientNew
        a.ShowDialog()

        If SelClientName <> "" Then
            Me.RClientName.Checked = True
            Me.txtClientNameSearch.Text = SelClientName
            FillClientsList()
        End If
    End Sub
End Class